<html>
<head><meta charset="utf-8"><title>Adding Types to loads/stores in Generated LLVM IR · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20Types.20to.20loads.2Fstores.20in.20Generated.20LLVM.20IR.html">Adding Types to loads/stores in Generated LLVM IR</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="245433888"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20Types%20to%20loads/stores%20in%20Generated%20LLVM%20IR/near/245433888" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chuyang Chen <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20Types.20to.20loads.2Fstores.20in.20Generated.20LLVM.20IR.html#245433888">(Jul 09 2021 at 12:04)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information/near/245289721">said</a>:</p>
<blockquote>
<p>I think it already has the information, although I'm not sure how accessible it is. You basically need the mir type system, which is a bit weak but is still good enough to determine the type of a place (and hence of the loads/stores to that place)</p>
</blockquote>
<p>Hi guys! I'm working on a GSoC project to integrate Enzyme, an auto-differentiation plugin based on LLVM, into Rust (if you're interesting in the details of the project, see <a href="https://docs.google.com/document/d/1__hNN3oPTyPAbNteYeaI3ghkKWdFYO4eFkt73cym800/edit?usp=sharing">here</a>). Enzyme needs the types of memory objects to do auto-differentiation. It is implemented by adding TBAA metadata to loads/stores to generated LLVM IR when with C/C++ and Swift. However, rustc won't generate such infomation and adding TBAA to rustc was thought as a too big change (see the <a href="#narrow/stream/131828-t-compiler/topic/TBAA.20Like.20Type.20Information/near/245289721">topic</a> I quote).</p>
<p>My original plan was to just leverage debug info generated by rustc, but it seems impracticable since retrieving types for loads/stores needs semantic information. So I need to modify rustc to type the loads/stores it generates (and thanks to <span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> who let me know that this information is already contained in MIR). <em>What I want to ensure is that adding type information to loads/stores in generated LLVM IR won't be a too big change to the compiler.</em> It would be very helpful if any compiler developer can give some opinions ("Is adding type infomation to loads/stores in generated LLVM IR accaptable?"). Other suggests are also welcome.</p>



<a name="245434125"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20Types%20to%20loads/stores%20in%20Generated%20LLVM%20IR/near/245434125" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20Types.20to.20loads.2Fstores.20in.20Generated.20LLVM.20IR.html#245434125">(Jul 09 2021 at 12:06)</a>:</h4>
<p>What does adding type information to loads/stores mean?</p>



<a name="245434156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20Types%20to%20loads/stores%20in%20Generated%20LLVM%20IR/near/245434156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20Types.20to.20loads.2Fstores.20in.20Generated.20LLVM.20IR.html#245434156">(Jul 09 2021 at 12:07)</a>:</h4>
<p>It is important that this does not collide with opaque pointer work.</p>



<a name="245435478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20Types%20to%20loads/stores%20in%20Generated%20LLVM%20IR/near/245435478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chuyang Chen <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20Types.20to.20loads.2Fstores.20in.20Generated.20LLVM.20IR.html#245435478">(Jul 09 2021 at 12:22)</a>:</h4>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> <a href="#narrow/stream/131828-t-compiler/topic/Adding.20Types.20to.20loads.2Fstores.20in.20Generated.20LLVM.20IR/near/245434125">said</a>:</p>
<blockquote>
<p>What does adding type information to loads/stores mean?</p>
</blockquote>
<p>For example, the Rust code is</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">a</span>: <span class="kt">u32</span> <span class="o">=</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"></span>
<span class="c1">// Some other code</span>
<span class="kd">let</span><span class="w"> </span><span class="n">b</span>: <span class="kp">&amp;</span><span class="kt">u32</span> <span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">a</span><span class="p">;</span><span class="w"></span>
<span class="c1">// Some other code</span>
<span class="kd">let</span><span class="w"> </span><span class="n">c</span>: <span class="kt">u32</span> <span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">b</span><span class="p">;</span><span class="w"></span>
</code></pre></div>
<p>and the last let statement generates LLVM IR</p>
<div class="codehilite" data-code-language="LLVM"><pre><span></span><code>  <span class="nv">%c</span> <span class="p">=</span> <span class="k">load</span> <span class="kt">i64</span><span class="p">,</span> <span class="kt">i64</span><span class="p">*</span> <span class="nv">%a</span><span class="p">,</span> <span class="k">align</span> <span class="m">8</span>
</code></pre></div>
<p>Type information may be added as (just an example, I haven't decided the actual format)</p>
<div class="codehilite" data-code-language="LLVM"><pre><span></span><code>  <span class="nv">%c</span> <span class="p">=</span> <span class="k">load</span> <span class="kt">i64</span><span class="p">,</span> <span class="kt">i64</span><span class="p">*</span> <span class="nv">%a</span><span class="p">,</span> <span class="k">align</span> <span class="m">8</span><span class="p">,!{</span><span class="nv">!"u32"</span><span class="p">,</span> <span class="nv">!"u32"</span><span class="p">}</span>
</code></pre></div>
<p>It describes the types of the source address and the target variable.</p>
<p>The type info will be metadata that has nothing to do with other parts of the compilation.</p>



<a name="245437578"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20Types%20to%20loads/stores%20in%20Generated%20LLVM%20IR/near/245437578" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20Types.20to.20loads.2Fstores.20in.20Generated.20LLVM.20IR.html#245437578">(Jul 09 2021 at 12:44)</a>:</h4>
<p>yeah, I don't see a problem with this as long as its disabled by default and/or has no measurable impact on compile times if enabled by default.</p>



<a name="245438330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20Types%20to%20loads/stores%20in%20Generated%20LLVM%20IR/near/245438330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Chuyang Chen <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20Types.20to.20loads.2Fstores.20in.20Generated.20LLVM.20IR.html#245438330">(Jul 09 2021 at 12:51)</a>:</h4>
<p><span class="user-mention silent" data-user-id="123586">nagisa</span> <a href="#narrow/stream/131828-t-compiler/topic/Adding.20Types.20to.20loads.2Fstores.20in.20Generated.20LLVM.20IR/near/245437578">said</a>:</p>
<blockquote>
<p>yeah, I don't see a problem with this as long as its disabled by default and/or has no measurable impact on compile times if enabled by default.</p>
</blockquote>
<p>OK, that's good.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>